package cn.zhengzhou.springbootmybatis.demo;

import java.io.FileOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class SaveObject {
    public static void saveObject(Object obj) throws Exception{
        //1.创建文件
        PrintStream printStream = new PrintStream(new FileOutputStream("E:/code2025/springboot-mybatis/src/main/resources/saveObject.txt", true));

        Class c = obj.getClass();
        String simpleName = c.getSimpleName();
        printStream.println("-----------"+simpleName+"-----------");
        //获取class对象所有字段
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
            //允许访问私有成员
            field.setAccessible(true);
            //获取字段名称
            String name = field.getName();
            //获取字段的值
            Object value = field.get(obj);
            printStream.println(name+"="+value);
        }
        printStream.close();
    }
}
